<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
    public function __construct()
    {
        parent::__construct();
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->payrollModel = $this->load->model('Payroll_Model');
        $this->timesheetModel = $this->load->model('Timesheet_Model');
        $this->load->helper('RequestManager');
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        $data = array();
    }


	public function get_rates($api){
		$data = array();
		$result = $this->clientModel->getRates($api);
		if($result != 0 ){
			$response["message"] = "success";
			$response["data"] = $result;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('index', $data);
	}
	
	public function view_user($api,$payroll_period_id,$user_id){
		$data = array();
		$result = $this->timesheetModel->getLogsByPeriodByUser($api, $payroll_period_id, $user_id);
		$payroll = $this->payrollModel->getPayrollPeriod($api,$payroll_period_id);
		if($result != 0 ){
			$response["message"] = "success";
			$response["data"] = $result;
			$response["period"] = $payroll;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('payroll', $data);
	}
	
	public function get_holidays($api){
		$data = array();
		$result = $this->clientModel->find($api);
		//$payroll = $this->payrollModel->getPayrollPeriod($api,$payroll_period_id);
		if(!empty($result)){
			$array = array('client_id'=>$result->id);
			$holidays = $this->clientModel->getClientHolidays($array);
			$response["message"] = "success";
			$response["data"] = $holidays;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('index', $data);
	}
	
	
    public function addclient($api)
    {
        $status = $this->input->post('status');
        
        $db_data = array(
            'name'      => $this->input->post('name'),
            'code'      => $this->input->post('code'),
            'status'    => in_array(strtolower($status), array('active','inactive','suspended')) ? strtolower($status) : 'inactive',
            'parent_id' => $this->input->post('parent_id')
        );
        
        $result = $this->clientModel->insertOrUpdate($db_data);
        
        if ($result) {
            $response['message'] = 'success';
        } else {
            $result = $this->clientModel->getResponse();
            $response['message'] = isset($result['message']) ? $result['message'] : 'failed';
        }
        
        echo ObjectUtils::arrayToJSON($response);
    }
    
    
    public function editclient($api)
    {
        $status = $this->input->post('status');
        
        $db_data = array(
            'id'        => $this->input->post('id'),
            'name'      => $this->input->post('name'),
            'code'      => $this->input->post('code'),
            'status'    => in_array(strtolower($status), array('active','inactive','suspended')) ? strtolower($status) : 'inactive',
            'parent_id' => $this->input->post('parent_id')
        );
        
        $result = $this->clientModel->insertOrUpdate($db_data);
        
        if ($result) {
            $response['message'] = 'success';
        } else {
            $result = $this->clientModel->getResponse();
            $response['message'] = isset($result['message']) ? $result['message'] : 'failed';
        }
        
        echo ObjectUtils::arrayToJSON($response);
    }
    
    
    public function get_client_list($api, $parent_client_id)
    {
        $result = $this->clientModel->getClientList($parent_client_id);
        
        $response['message'] = 'success';
        $response['data'] = $result;
        
        echo ObjectUtils::arrayToJSON($response);
    }
    
    
    public function get_client_details($api)
    {
        // get required param
        $client_id = $this->input->post('client_id');
        $result = $this->clientModel->getClientDetails($client_id);
        
        $response['message'] = count($result) ? 'success' : 'failed';
        $response['data'] = $result;
        
        echo ObjectUtils::arrayToJSON($response);
    }
	
}